package com.suixun.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

import com.suixun.service.BaseService;

public class BaseServiceImpl implements BaseService {

	/**
	 * 导出excel
	 */
	@Override
	public HSSFWorkbook exportList(String title, ArrayList<String> listParm, List<List<String>> listValue)
			throws Exception {
		long start = System.currentTimeMillis();
		// 创建新的Excel 工作簿
		HSSFWorkbook workbook = new HSSFWorkbook();
		if (listParm == null || listValue == null)
			return workbook;
		// 在Excel工作簿中建一工作表，其名为缺省值
		HSSFSheet sheet = workbook.createSheet();
		// 在工作薄中建立工作表
		workbook.setSheetName(0, title);// 设置表格标题及编码格式
		// 在索引0的位置创建行（最顶端的行）
		HSSFCell cell = null;
		HSSFRow row = sheet.createRow((short) 0);
		// 设置字体颜色为红色
		HSSFCellStyle style = workbook.createCellStyle();
		HSSFFont font = workbook.createFont();
		font.setFontHeightInPoints((short) 12);// 字号
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
		String[] arrys1 = new String[listParm.size()];// 表格题头
		listParm.toArray(arrys1);
		for (int i = 0; i < listParm.size(); i++) {
			if ("学校列表".contains(title)) {
				sheet.setColumnWidth(i, 11000);
			} else {
				sheet.setColumnWidth(i, 4500);
			}
			cell = row.createCell(Short.parseShort((i) + ""));
			cell.setCellValue(arrys1[i]);
			style.setFont(font);
			cell.setCellStyle(style);
		}
		for (int i = 0; i < listValue.size(); i++) {
			row = sheet.createRow(i + 1);
			List<String> map = listValue.get(i);
			for (int j = 0; j < map.size(); j++) {
				if ("学校列表".contains(title)) {
					sheet.setColumnWidth(i, 11000);
				} else {
					sheet.setColumnWidth(i, 4500);
				}
				cell = row.createCell(Short.parseShort(j + ""));
				cell.setCellValue(map.get(j));
				font.setFontHeightInPoints((short) 11);// 字号
				style.setFont(font);
				cell.setCellStyle(style);
			}
		}
		return workbook;
	}

	/**
	 * 导出Excel（带示例）
	 */
	@Override
	public HSSFWorkbook exportListByExample(String title, ArrayList<String> listParm, ArrayList<String> listExample,
			List<List<String>> listValue) throws Exception {
		// 创建新的Excel 工作簿
		HSSFWorkbook workbook = new HSSFWorkbook();
		if (listParm == null || listValue == null)
			return workbook;
		// 在Excel工作簿中建一工作表，其名为缺省值
		HSSFSheet sheet = workbook.createSheet();
		// 在工作薄中建立工作表
		workbook.setSheetName(0, title);// 设置表格标题及编码格式
		// 在索引0的位置创建行（最顶端的行）
		HSSFCell cell = null;
		HSSFRow row1 = sheet.createRow((short) 0);

		String[] arrys1 = new String[listParm.size()];// 表格题头
		listParm.toArray(arrys1);
		for (int i = 0; i < listParm.size(); i++) {
			sheet.setColumnWidth(i, 5500);
			cell = row1.createCell(Short.parseShort((i) + ""));
			cell.setCellValue(arrys1[i]);
			// 设置字体颜色为红色
			HSSFCellStyle style = workbook.createCellStyle();
			HSSFFont font = workbook.createFont();
			font.setColor(HSSFColor.RED.index);
			font.setFontHeightInPoints((short) 12);// 字号
			font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);// 加粗
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
			style.setFont(font);
			cell.setCellStyle(style);
		}

		HSSFRow row2 = sheet.createRow((short) 1);
		String[] arrys2 = new String[listExample.size()];// 表格示例
		listExample.toArray(arrys2);
		for (int i = 0; i < listExample.size(); i++) {
			sheet.setColumnWidth(i, 5500);
			cell = row2.createCell(Short.parseShort((i) + ""));
			cell.setCellValue(arrys2[i]);
			// 设置字体颜色为红色
			HSSFCellStyle style = workbook.createCellStyle();
			HSSFFont font = workbook.createFont();
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
			style.setFont(font);
			cell.setCellStyle(style);
		}

		for (int i = 0; i < listValue.size(); i++) {
			row1 = sheet.createRow(i + 1);
			row2 = sheet.createRow(i + 1);
			List<String> map = listValue.get(i);
			for (int j = 0; j < map.size(); j++) {
				sheet.setColumnWidth(i, 5500);
				cell = row1.createCell(Short.parseShort(j + ""));
				cell = row2.createCell(Short.parseShort(j + ""));
				cell.setCellValue(map.get(j));
				// 设置字体颜色为红色
				HSSFCellStyle style = workbook.createCellStyle();
				HSSFFont font = workbook.createFont();
				font.setFontHeightInPoints((short) 11);// 字号
				style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
				style.setFont(font);
				cell.setCellStyle(style);
			}
		}

		return workbook;
	}

}
